package com.hwadee.springboot_lx.controller;

import com.hwadee.springboot_lx.entity.Log;
import com.hwadee.springboot_lx.entity.LogSearchCriteria;
import com.hwadee.springboot_lx.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/logs")
public class LogController {

    @Autowired
    private RedisService redisService;

    @PostMapping("/search")
    public List<Log> searchLogs(@RequestBody LogSearchCriteria criteria) {
        return redisService.searchLogs(criteria);
    }

    @GetMapping("/export/{logId}")
    public ResponseEntity<byte[]> exportLog(@PathVariable Long logId) {
        byte[] logData = redisService.exportLog(logId);
        return ResponseEntity.ok()
                .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + logId + ".txt\"")
                .body(logData);
    }

    @GetMapping("/exportAll")
    public ResponseEntity<byte[]> exportAllLogs() {
        byte[] allLogs = redisService.exportAllLogs();
        return ResponseEntity.ok()
                .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"logs.zip\"")
                .body(allLogs);
    }
}



